package com.atcjx.controller;

import com.atcjx.MyUtils.ResultFactory;
import com.atcjx.exception.UserException;
import com.atcjx.pojo.Article.ArticleVO;
import com.atcjx.pojo.Result;
import com.atcjx.service.CategoryServiceImpl;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author admin
 * @version 1.0.0
 * @ClassName CategoryController.java
 * @Description TODO
 * @createTime 2021年09月21日 19:02:00
 */
@RestController
@Api("类别选择文章")
public class CategoryController {
    @Autowired
    CategoryServiceImpl categoryService;


    @ApiOperation("获取所有文章的分类信息即其类别的文章数")
    @GetMapping("/categories")
    public Result getCategories() {
        return ResultFactory.successWithData(categoryService.getAllCategories());
    }

    @ApiOperation("添加一个新的主题")
    @GetMapping("/categories/{category}/{theme}")
    @ApiImplicitParam(name = "category", value = "标签名称", required = true, dataType = "String", paramType = "path")
    public Result createTag(
            @PathVariable("category") String category,
            @PathVariable(value = "theme", required = false) String theme) throws UserException {
        if (Objects.equals(theme, "")||theme.equals("null"))
            theme = "未分类";
        return ResultFactory.success(categoryService.createCategory(category, theme));
    }

    @ApiOperation("添加一个分类")
    @GetMapping("/categories/add/{categoryName}")
    public Result createCategory(@PathVariable(value = "categoryName", required = true) String categoryName){
        return ResultFactory.success(categoryService.createCategoryOnly(categoryName));
    }
    @ApiOperation("删除一个主题")
    @DeleteMapping("/theme/{theme}")
    public Result deleteCategory(
            @PathVariable String theme) {

        return ResultFactory.success(categoryService.deleteCategory(theme));
    }
    @ApiOperation("更新一个主题")
    @PutMapping("/theme/{oldTheme}/{newTheme}")
    public Result toUpdateTheme(
            @PathVariable String newTheme, @PathVariable String oldTheme) {
        return ResultFactory.success(categoryService.updateThemeByON(newTheme,oldTheme));
    }
    @ApiOperation("删除一个分类")
    @DeleteMapping("/categories/{category}")
    public Result deleteTheme(
             @PathVariable String category) {
        return ResultFactory.success(categoryService.deleteTheme(category));
    }
    @ApiOperation("更新一个分类的名称")
    @PutMapping("/categories/{oldCategory}/{newCategory}")
    public Result updateTheme(
            @PathVariable String oldCategory,
            @PathVariable String newCategory) {
        return ResultFactory.success(categoryService.updateTheme(oldCategory,newCategory));
    }

    @PutMapping("/updateThemeToCategory/{theme}/{category}")
    public Result updateThemeToCategory(@PathVariable String theme,
                                        @PathVariable String category){
        return ResultFactory.success(categoryService.updateThemeCategory(theme,category));
    }
}
